home *** CD-ROM | disk | FTP | other *** search
- Path: gate.net!pslfl2-12
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.perl
- Subject: Re: Stupid array problems
- Date: 14 Jan 1996 17:44:01 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4dbfd1$2hpc@news.gate.net>
- References: <4d9b9v$14n@paperboy.ids.net>
- NNTP-Posting-Host: pslfl2-12.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4d9b9v$14n@paperboy.ids.net>,
- scarney@conan.ids.net (scarney) wrote:
- >Ok, I've been having what I thought would be a simple problem but no one
- >seems to quite figure out. I Have an array of strings, both the key and
- >the string being pointers. Putting information onto the array is like a
- >stack. My problem comes in the removal of information from it. Popping
- >from a stack only pulls the lastelement out of the array, I want to pull
- >any element out. Destroying the data isn't the hard part because I just
- >blow up the pointers...the problem is that the indexing for the array
- >gets kind of screwy. If I have an array organized by integers with
- >elements 1 2 3 4 5 6 7 8, if I decided to kill element #4 I'm going to
- >have an array of 1 2 3 5 6 7 8, and as time goes on its going to get more
- >screwy. Is there an easy way to delete an element while preserving some
- >semblance of sequence in the array indexing? In Perl I'd use the splice()
- >command...is there anything similar in C/C++?
- >
- >I get the feeling I'm looking at this from a completely wrong
- >perspective, so don't flame me if I am...
- >
- >Thanks.
- >-Seth
- >
-
- If you want to maintain the same order in the array you only need to move the
- pointers. Assuming you indicate the end with a NULL pointer and you need to
- free the string and *i* indexes the pointer to the string to remove it might
- go something like this:
-
- free(array[i]);
- for(;array[i];array[i]=array[i+1],i++);
-
- This moves all pointers beyond the pointer you're removing *down one*
- including the NULL.
-
- Bill
-
- "Whatcha got on?...Your mind?"
-